using System.Collections.Generic;
using System.Linq;

namespace WineCellar.Web.Helpers
{
    /// <summary>
    /// Class used to prepare a column and filter model for a ExtJs grid
    /// </summary>
    public class ExtJsGridModel
    {
        #region Private declarations
        private ICollection<ExtFilter> extFilters = new List<ExtFilter>();
        private ICollection<ExtColumnModel> extColumnModels = new List<ExtColumnModel>();
        private ICollection<ExtRecord> extRecords = new List<ExtRecord>();
        #endregion

        #region Properties
        /// <summary>
        /// Returns the ext grid filters.
        /// </summary>
        /// <value>The ext filters.</value>
        public ExtFilter[] ExtFilters
        {
            get { return extFilters.ToArray(); }
        }
        /// <summary>
        /// Gets the ext grid jcolumn models.
        /// </summary>
        /// <value>The ext column models.</value>
        public ExtColumnModel[] ExtColumnModels
        {
            get { return extColumnModels.ToArray(); }
        }

        /// <summary>
        /// Gets the ext records
        /// </summary>
        public ExtRecord[] ExtRecords
        {
            get { return extRecords.ToArray(); }
        }
        #endregion
        #region Public methods

        /// <summary>
        /// Adds a column model and a column filter to this object's arrays
        /// </summary>
        /// <param name="dataIndex">the dataIndex.</param>
        /// <param name="header">The header.</param>
        /// <param name="dataType">Type of the data.</param>
        public void Add(string dataIndex, string header, string dataType)
        {
            Add(dataIndex, header, dataType, "left", dataIndex);
        }

        /// <summary>
        /// Adds a column model and a column filter to this object's arrays 
        /// </summary>
        /// <param name="dataIndex">the dataIndex.</param>
        /// <param name="header">The header.</param>
        public void Add(string dataIndex, string header)
        {
            Add(dataIndex, header, "string", "left", dataIndex);
        }

        public void Add(string dataIndex, string header, string dataType, string align)
        {
            Add(dataIndex, header, "string", align, dataIndex);
        }

        /// <summary>
        /// Adds a column model and a column filter to this object's arrays
        /// </summary>
        /// <param name="dataIndex">dataIndex</param>
        /// <param name="header">header</param>
        /// <param name="dataType">datatype</param>
        /// <param name="align">alignment</param>
        /// <param name="mapping">mapping for associations</param>
        public void Add(string dataIndex, string header, string dataType, string align, string mapping)
        {
            extFilters.Add(new ExtFilter { DataIndex = dataIndex, DataType = dataType });
            extColumnModels.Add(new ExtColumnModel { DataIndex = dataIndex, Header = header, Align = align });

            if (extRecords.Count == 0)
                extRecords.Add(new ExtRecord { Name = "Id", Mapping = "Id" });

            extRecords.Add(new ExtRecord { Name = dataIndex, Mapping = mapping });
        }

        public void AddHidden(string dataIndex)
        {
            extRecords.Add(new ExtRecord { Name = dataIndex, Mapping = dataIndex });
        }

        #endregion
    }
}